package com.jcl.recruit.timer.crawler.service.impl;

import com.google.gson.Gson;
import com.jcl.recruit.resume.model.ResumeOrig;
import com.jcl.recruit.searchresume.model.SearchResume;
import com.jcl.recruit.searchresume.model.SearchResumeEdu;
import com.jcl.recruit.searchresume.model.SearchResumeWork;
import com.jcl.recruit.timer.crawler.bean.JxBean;
import com.jcl.recruit.timer.crawler.bean.JxResumeBean;
import com.jcl.recruit.timer.crawler.bean.LagouJsonBean;
import com.jcl.recruit.timer.crawler.service.CrawlerChannelResumeJx;
import org.apache.commons.lang3.StringUtils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName crawlerChannelResumeTaskService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/30 15:57
 * @Version 1.0
 **/
public class LagouCrawlerResumeTask implements CrawlerChannelResumeJx {

    // 匹配中文
    private static String reg = "[^\u4e00-\u9fa5]";
    private static String htmlReg = "<[^>]+>";


    @Override
    public JxBean crawlerResume(List<ResumeOrig> resumeOrigList) throws Exception{
        JxBean jxBean = new JxBean();
        List<JxResumeBean> jxResumeBeans = new ArrayList<>();
        jxBean.setJxResumeBeans(jxResumeBeans);

        for(ResumeOrig orig: resumeOrigList) {

            jxBean.setExternalId(orig.getId());
            JxResumeBean jxResumeBean = new JxResumeBean();
            jxResumeBeans.add(jxResumeBean);

            LagouJsonBean lagouJsonBean = new Gson().fromJson(orig.getContent(), LagouJsonBean.class);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            SearchResume searchResume = new SearchResume();
            if (orig.getRealname().contains("-")) {
                orig.setRealname(orig.getRealname().substring(0, orig.getRealname().indexOf("-")));
            }
            searchResume.setRealname(orig.getRealname().replaceAll(reg, "").trim());
            searchResume.setSex(lagouJsonBean.getSex());
            searchResume.setBirthday(StringUtils.isBlank(lagouJsonBean.getBirthday()) ? null : sdf.parse(lagouJsonBean.getBirthday().replace(".", "-"), new ParsePosition(0)));
            searchResume.setNativePlace(lagouJsonBean.getCity());
            searchResume.setCurrentCity(lagouJsonBean.getCity());
            searchResume.setCurrentAddress(lagouJsonBean.getCity());
            searchResume.setEvaluate(lagouJsonBean.getMyRemark());
            if (lagouJsonBean.getExpectJob() != null && StringUtils.isNotBlank(lagouJsonBean.getExpectJob().getSalarys())) {
                searchResume.setDesireSalaryStart(Integer.valueOf(lagouJsonBean.getExpectJob().getSalarys().substring(0, lagouJsonBean.getExpectJob().getSalarys().indexOf("k"))) * 1000);
                if (lagouJsonBean.getExpectJob().getSalarys().contains("-")) {
                    searchResume.setDesireSalaryEnd(Integer.valueOf(lagouJsonBean.getExpectJob().getSalarys().substring(lagouJsonBean.getExpectJob().getSalarys().indexOf("-") + 1, lagouJsonBean.getExpectJob().getSalarys().length() - 1)) * 1000);
                }
            }
            if (lagouJsonBean.getWorkYear().equals("10年以上")) {
                searchResume.setWorkyear(10);
            } else {
                searchResume.setWorkyear(Integer.valueOf(lagouJsonBean.getWorkYear().substring(0, lagouJsonBean.getWorkYear().length() - 1)));
            }
            if (lagouJsonBean.getWorkexperiences() != null) {
                searchResume.setWorkLastPosition(lagouJsonBean.getWorkexperiences().get(0).getPositionname().replaceAll(htmlReg, ""));
                searchResume.setWorkLastCompany(lagouJsonBean.getWorkexperiences().get(0).getCompanyname());
                searchResume.setWorkDetail(lagouJsonBean.getWorkexperiences().get(0).getWorkcontent());
            }
            if (lagouJsonBean.getEducationexperiences() != null) {
                searchResume.setEduLastEducation(lagouJsonBean.getEducationexperiences().get(0).getEducation());
                searchResume.setEduLastSchool(lagouJsonBean.getEducationexperiences().get(0).getSchoolname());
                searchResume.setEduLastMajor(lagouJsonBean.getEducationexperiences().get(0).getProfessional());
            }
            searchResume.setSourceSub("lagou");
            searchResume.setCreatetime(new Date());
            searchResume.setUpdateDate(searchResume.getCreatetime());
            searchResume.setSource(0);
            searchResume.setStatus(0);
            searchResume.setKeyword(orig.getKeyword());

            jxResumeBean.setSearchResume(searchResume);

            List<LagouJsonBean.Educationexperiences> educationexperiences = lagouJsonBean.getEducationexperiences();
            List<SearchResumeEdu> edus = new ArrayList<>();
            if (educationexperiences != null) {
                for (LagouJsonBean.Educationexperiences entity : educationexperiences) {
                    SearchResumeEdu edu = new SearchResumeEdu();
                    edu.setSchool(entity.getSchoolname());
                    edu.setMajor(entity.getProfessional());
                    edu.setDegree(entity.getEducation());
                    edu.setStartDate(entity.getStartdate());
                    edu.setEndDate(entity.getEnddate());
                    edus.add(edu);
                }
            }
            List<LagouJsonBean.Workexperiences> workexperiences = lagouJsonBean.getWorkexperiences();
            List<SearchResumeWork> works = new ArrayList<>();
            if (workexperiences != null) {
                for (LagouJsonBean.Workexperiences entity : workexperiences) {
                    SearchResumeWork searchResumeWork = new SearchResumeWork();
                    searchResumeWork.setCompany(entity.getCompanyname());
                    searchResumeWork.setDuty(StringUtils.isNotBlank(entity.getWorkcontent()) ? entity.getWorkcontent().replaceAll("&nbsp;", "") : null);
                    searchResumeWork.setPosition(entity.getPositionname().replaceAll(htmlReg, ""));
                    searchResumeWork.setStartDate(entity.getStartdate());
                    searchResumeWork.setEndDate(entity.getEnddate());
                    works.add(searchResumeWork);
                }
            }

            jxResumeBean.setResumeWorks(works);
            jxResumeBean.setSearchResumeEdus(edus);

        }

        return jxBean;
    }
}
